//
//  MBProgressHUD+CRMHUD.m
//  BaibuSeller
//
//  Created by  bb on 16/7/28.
//  Copyright © 2016年 whawhawhat. All rights reserved.
//

#import "MBProgressHUD+CRMHUD.h"

@implementation MBProgressHUD (CRMHUD)
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].delegate window];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeText;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.userInteractionEnabled = YES;
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5];
}



/**
 *  显示成功信息
 *
 *  @param success 信息内容
 */
+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 *  @param view    显示信息的视图
 */
+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

/**
 *  显示错误信息
 *
 */
+ (void)showError:(NSString *)error
{
    UIView *view = [[UIApplication sharedApplication].delegate window];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (error.length < 15) {
        hud.label.text = error;
    }else{
        hud.detailsLabel.text = error;
    }
    hud.mode = MBProgressHUDModeText;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:view];
    });

}

/**
 *  显示错误信息
 *
 */
+ (void)showError:(NSString *)error ForView:(UIView *)view {
    if (!view) {
        return;
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (error.length < 15) {
        hud.label.text = error;
    }else{
        hud.detailsLabel.text = error;
    }
    hud.mode = MBProgressHUDModeText;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:view];
    });

}

/**
 *  简单的提示信息 比如没数据了
 *
 */
+ (void)showText:(NSString *)text
{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (text.length < 15) {
        hud.label.text = text;
    }else{
        hud.detailsLabel.text = text;
    }
    hud.mode = MBProgressHUDModeText;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:view];
    });
    
}


/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

/**
 *  显示错误信息
 *
 *  @param message 信息内容
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil];
}

/**
 *  显示一些信息
 *
 *  @param message 信息内容
 *  @param view    需要显示信息的视图
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].delegate window];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    if (view) {
    hud.userInteractionEnabled = YES;//此处可以由外界控制  和view混合使用
    }

    return hud;
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].delegate window];
    [self hideHUDForView:view animated:NO];
}


/**
 *  show成功或者失败
 *  自动消失 消失后回调block
 *  block回调
 */
+ (void)MBPShowMessage:(BOOL)success Message:(NSString *)message DismissBlock:(void(^)(void) )dismissBlock {
    //    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    if (success) {
        
        [MBProgressHUD showSuccess:message];
    }else{
        [MBProgressHUD showError:message];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (dismissBlock) {
            dismissBlock();
        }
        [MBProgressHUD hideHUD];
    });
}

/**
 *  show成功或者失败
 *  自动消失 消失后回调block
 *  block回调
 */
+ (void)MBPShowMessage:(BOOL)success Message:(NSString *)message toView:(UIView *)view DismissBlock:(void(^)(void) )dismissBlock {
    //    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    if (success) {
        
        [MBProgressHUD showSuccess:message toView:view];
    }else{
        [MBProgressHUD showError:message toView:view];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (dismissBlock) {
            dismissBlock();
        }
        [MBProgressHUD hideHUDForView:view];
    });
}



@end
